<template>
  <div>
    <x-header :left-options="{backText: ''}" style="background-color:#31b9b8;">迎新招生</x-header>
    <group title="登录账号密码与PC版一致">
      <x-input title="账号" type="text" v-model="loginname" placeholder="请输入考生号或工号" required></x-input>
      <x-input title="密码" type="password" v-model="password" placeholder="请输入密码" required></x-input>
      <div style="padding: 15px">
        <p style="color: red; :display: display1">{{resultText}}</p>
        <br/>
        <x-button type="primary" @click.native="toLogin" action-type="text" :disabled="!loginname||!password"
                  style="background-color: #31b9b8">登录
        </x-button>
      </div>
    </group>
  </div>
</template>
<script>
  import {XHeader, XInput, Group, XButton, AjaxPlugin} from 'vux'
  export default {
    components: {
      XInput,
      XHeader,
      Group,
      XButton
    },
    data () {
      return {
        loginname: '',
        password: '',
        display1: 'none',
        resultText: ''
      }
    },
    mounted () {
      this.autoLogin()
    },
    methods: {
      toLogin: function () {
        let dataStr = 'loginname=' + this.loginname + '&password=' + this.password
        AjaxPlugin.$http.post('http://121.40.193.122:8080/loginValidate', dataStr)
          .then((response) => {
            console.log(response.data)
            console.log(this)
            if (response.data.code === -1) {
              this.resultText = '登录失败，未知错误!'
              this.display1 = 'block'
              return
            }
            if (response.data.data == null) {
              this.resultText = '用户名或密码错误!'
              this.display1 = 'block'
              return
            }
            if (response.data.data.usertype === 14) {
              this.$router.push('/index')
            } else {
              this.$router.push('/index_teacher')
            }
          })
      },
      autoLogin: function () {
        let loginName = this.$route.params.loginName
        if (loginName == null) {
          return
        }
        let dataStr = 'loginname=' + loginName
        AjaxPlugin.$http.post('http://121.40.193.122:8080/validateNoPassword', dataStr)
          .then((response) => {
            if (response.data.code === -1) {
              this.resultText = '登录失败，未知错误!'
              this.display1 = 'block'
              return
            }
            if (response.data.data == null) {
              this.resultText = '认证失败!'
              this.display1 = 'block'
              this.$router.push('/login')
              return
            }
            if (response.data.data.usertype === 14) {
              this.$router.push('/index')
            } else {
              this.$router.push('/index_teacher')
            }
          })
      }
    }
  }
</script>
